<template>
  <!-- 父组件---->
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 导入上传 Excel 子组件  -->
        <upload-excel
          :on-success="handleSuccess"
        />
      </el-card>
    </div>
  </div>
</template>

<script>
// 导入上传的组件
import UploadExcel from '@/components/UploadExcel/index.vue'

// 导入时间转换
// import { formatExcelDate } from '@/utils'

// 导入 批量导入员工 接口API
import { importEmployeeAPI } from '@/api/employees'

export default {
  name: 'Excel',
  // 绑定上传组件
  components: { UploadExcel },
  methods: {
    transExcel(results) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }

      results.map(item => {
        const obj = {}

        const transKeys = Object.keys(item)
        transKeys.forEach(key => {
          obj[userRelations[key]] = item[key]
        })

        return obj
      })
    },
    // 成功调用之后的函数，它会返回表格的表头和内容。
    async handleSuccess({ results, header }) {
      /* results = [{
        入职日期: 44142,
        姓名: "百里",
        工号: 777,
        手机号: "18222223322",
        转正日期: 44142,
        部门: "总裁办"
      }]
        */
      // 表头-header--->['姓名', '手机号', '入职日期', '转正日期', '工号', '部门']
      const arr = this.transExcel(results)

      console.log(arr)
      /* arr = [{
        correctionTime: 44142,
        departmentName: "总裁办",
        mobile: "18222223322",
        timeOfEntry: 44142,
        username: "百里",
        workNumber: 777,
      }] */
      //   console.log(results)
      //   console.log(header)

      // 批量导入员工--调接口-跳转路由-到员工管理页面-提示用户
      const res = await importEmployeeAPI(arr)
      this.$router.back()
      this.$message.success(res.message)
    }

  }
}
</script>
